package com.ch.control;



import com.ch.Result;
import com.ch.entity.User;
import com.ch.service.UserService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserControl {
    @Autowired
    private UserService userService;
    @GetMapping("login/{username}/{pwd}")
    public Result login(@PathVariable String username, @PathVariable String pwd){
        User user= userService.login(username,pwd);
        if (user!=null){
            return Result.success(user);
        }
        else {
            return Result.error();
        }
    }
    @PostMapping("adduser")
    public Result insert(@RequestParam String userName,@RequestParam String pwd){
        User user=new User(userName,pwd);
        if (userService.insert(user)){
            return Result.success();
        }
        else {
            return Result.error();
        }
    }
    @GetMapping("getbyid/{id}")
    public User getbyid(@PathVariable int id){
        User user = userService.getbyid(id);
        return user;
    }
}
